package com.sunsharing.flow.exe;

import com.sunsharing.common.Context;
import com.sunsharing.common.ContextCreate;
import com.sunsharing.common.utils.StringUtils;
import com.sunsharing.flow.event.NodeLeaveEvent;
import com.sunsharing.flow.event.ProcessInstanceCreateEvent;
import com.sunsharing.flow.eventman.GlobalObject;
import com.sunsharing.flow.model.ProcessDefinition;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: criss
 * Date: 13-5-5
 * Time: 下午3:56
 * To change this template use File | Settings | File Templates.
 */
public class ProcessInstance implements Serializable {

    private String startTime;
    private String endTime;
    private String  pid;
    private Token rootToken;
    transient ProcessDefinition pro;
    /**
     * 功能描述:
     *    根据一个给定的工作流定义创建一个新的工作流实例
     * @param processDefinition ProcessDefinition
     */
    public ProcessInstance(ProcessDefinition processDefinition,Context context) {

        if (processDefinition == null) {
            throw new NullPointerException(
                    "工作流定义为空,不能创建工作流实例!");
        }

        this.pid = processDefinition.getId();
        this.rootToken = new Token(this, processDefinition.getStartNode());
        this.rootToken.setContext(context);
        this.startTime = StringUtils.getCurrentTime();

        //流程实例创建事件
        ExecutionContext executeContext = new ExecutionContext(rootToken);
        ProcessInstanceCreateEvent event = new ProcessInstanceCreateEvent(
                executeContext);
        //WorkFlowService.getFlow(pid)
        event.setEventSource(processDefinition);
        processDefinition.multicastEvent(event);


    }

    public void end()
    {
        //Context restContext = ContextCreate.createResultContext("流程结束了", context);
        //new Dispatch().run(restContext);
        //流程实例创建事件
        ExecutionContext executeContext = new ExecutionContext(rootToken);
        NodeLeaveEvent event = new NodeLeaveEvent(GlobalObject.obj);
        event.setExecutionContext(executeContext);
        ProcessDefinition pd = WorkFlowService.getFlow(pid);
        pd.getEndNode().multicastEvent(event);
        return;
    }

    /**
     * 启动
     */
    public void start()
    {
        this.getRootToken().signal();
    }

    public void signal()
    {
        this.getRootToken().signal();
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public ProcessDefinition getDefinition() {
        if(pro==null)
        {
            pro = WorkFlowService.getFlow(pid);
        }
        return pro;
    }


    public Token getRootToken() {
        return rootToken;
    }

    public void setRootToken(Token rootToken) {
        this.rootToken = rootToken;
    }

}
